package shuashuami.hb.com.avtivity.agent;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.avtivity.AbActivity;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.view.SelectBankDialog;

public class AgentAddYinHangKaActivity extends AbActivity {

    private EditText edtChikaren, edtShenfenzheng, edtKahao, edtZhihang, tvKaihudi;
    private TextView tvXuanzeyinhang;
    private Button btnSave;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_add_yin_hang_ka);
        edtChikaren = (EditText) findViewById(R.id.edt_cshenqing_add_yinhangka_name);
        edtShenfenzheng = (EditText) findViewById(R.id.edt_cshenqing_add_yinhangka_shenfenzheng);
        edtKahao = (EditText) findViewById(R.id.edt_cshenqing_add_yinhangka_kahao);
        edtZhihang = (EditText) findViewById(R.id.edt_cshenqing_add_yinhangka_kaihuzhihang);
        tvXuanzeyinhang = (TextView) findViewById(R.id.edt_cshenqing_add_yinhangka_yinhang_name);
        tvKaihudi = (EditText) findViewById(R.id.edt_cshenqing_add_yinhangka_kaihudi);
        btnSave = (Button) findViewById(R.id.btn_cshenqing_add_zhifubao_save);

    }

    @Override
    protected void initData() {
        setTitleBar("银行卡账户");
        setLeftView();
        getYinhangMessage();
    }

    @Override
    protected void initListener() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        tvXuanzeyinhang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectBankDialog = new SelectBankDialog(AgentAddYinHangKaActivity.this, tvXuanzeyinhang);
                selectBankDialog.show();
            }
        });
    }

    SelectBankDialog selectBankDialog;

    public void save() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        HttpAgentMethods.saveYinhangka(listener,
                edtChikaren.getText().toString(),
                edtShenfenzheng.getText().toString(),
                edtKahao.getText().toString(),
                selectBankDialog.getId(),
                tvKaihudi.getText().toString(),
                edtZhihang.getText().toString());
    }

    ProgressDialog dialog2;
    //获取银行卡信息
    public void getYinhangMessage() {
        dialog2 = ProgressDialog.show(this, "提示", "加载中...");
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        HttpAgentMethods.getBank(listener);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status")==203) {
                            LoginHttp loginHttp = new LoginHttp(AgentAddYinHangKaActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(AgentAddYinHangKaActivity.this, jsonObject.getString("message"));
                            setResult(RESULT_CANCELED);
                            finish();
                        } else {
                            ToastUtil.showShort(AgentAddYinHangKaActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    dialog2.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status")==203) {
                            LoginHttp loginHttp = new LoginHttp(AgentAddYinHangKaActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject content = jsonObject.getJSONObject("content");
                            edtChikaren.setText(content.getString("bankcard_name"));
                            edtShenfenzheng.setText(content.getString("bankcard_identify"));
                            edtKahao.setText(content.getString("bankcard_no"));
                            tvKaihudi.setText(content.getString("bankcard_address"));
                            edtZhihang.setText(content.getString("bankcard_info"));
                        } else {
                            ToastUtil.showShort(AgentAddYinHangKaActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }

            super.handleMessage(msg);
        }
    };
}
